package com.gopay.webtest.handler;

import com.gopay.webtest.util.BaseSign;
import com.gopay.webtest.util.MapUtil;
import com.gopay.webtest.util.ReflectUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * 基金签名类
 * @FundSignHandler.java
 * @author wangdong
 * @2016-4-21 上午10:16:43  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("fundSignHandler")
public class FundSignHandler extends BaseSign {
    @Override
    public String execute(Object obj, List<String> signFields) throws Exception {

        if (signFields == null || signFields.size() == 0)
            throw new Exception("组装加签明文失败，加签域");
        Map<String,String> resultMap = new HashMap<String,String>();
        //将所有空数据默认为空字符串
        for (String singField : signFields) {
            if("signType".equals(singField) || "charset".equals(singField)){
                continue;
            }
            String val = ReflectUtil.getFieldValue(obj, singField);
            if (StringUtils.isNotBlank(val)){
                resultMap.put(singField, val);
            }else{
                resultMap.put(singField, "");
            }
        }
        String s = MapUtil.sortStringByKey(resultMap, "&");
        return s;
    }
}
